package com.studio.framework.controller.monitorSystem.controller;

import com.studio.framework.controller.monitorSystem.bean.ServerList;
import com.studio.framework.controller.monitorSystem.dto.ServerDto;
import com.studio.framework.controller.monitorSystem.service.ServerGroupService;
import com.studio.framework.controller.monitorSystem.service.ServerService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Author jujh
 * @Date 2018/7/24
 * 服务器管理
 */
@Controller
public class ServerController {

    @Autowired
    private ServerService serverService;

    @Autowired
    private ServerGroupService serverGroupService;

    @RequestMapping(value = "/server-list")
    public ModelAndView pageJump() {
        ModelAndView mv = new ModelAndView("server-list");
        return mv;
    }


    @RequestMapping(value = "/server-list1")
    @ResponseBody
    public String findAll() {
        List<ServerList> serverList = serverService.findAllByCondition();
        JSONArray json = JSONArray.fromObject(serverList);
        return json.toString();
    }

    /**
     * add
     *
     * @return
     */
    @RequestMapping(value = "/create-server")
    @ResponseBody
    public String create(ServerDto createServerDto) {
        serverService.create(createServerDto);
        return "ok";
    }

    /**
     * update
     *
     * @return
     */
    @RequestMapping(value = "/update-server")
    @ResponseBody
    public String update(ServerDto serverDto) {
        int flag = serverService.update(serverDto);
        if (flag == 0) {
            return "ok";
        } else {
            return "error";
        }
    }

    /**
     * delete
     *
     * @return
     */
    @RequestMapping(value = "/delete-server")
    @ResponseBody
    public String delete(String id) {
        int flag = serverService.delete(id);
        if (flag == 0) {
            return "ok";
        } else {
            return "error";
        }
    }
}
